package dev.kaayf.examples;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ComponentCaller;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;

import java.util.Date;

import dev.kaayf.db.NoteDb;
import dev.kaayf.utils.CoverUploader;
import dev.kaayf.utils.Note;

public class CreateActivity extends AppCompatActivity {

    private ImageButton createNote;
    private ImageView noteCover, backButton;
    private EditText noteName;
    private static final int TEXT_MAX_LENGTH = 16;
    private View imageViewBox;
    CoverUploader coverUploader;

    String imagePath;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_create);
        findViews();

        NoteDb noteDb = new NoteDb(this);

        coverUploader = new CoverUploader(this);

        // upload note cover touch event
        noteCover.setOnTouchListener((view, motionEvent) -> {
            coverUploader.selectImage();
            imageViewBox = view;
            return false;
        });

        // create button click listener
        createNote.setOnClickListener(v -> {
            String name = noteName.getText().toString();
            if (name.isEmpty() || name.length() > TEXT_MAX_LENGTH) {
                // note name is invalid
                noteName.setText("");
                Toast.makeText(CreateActivity.this, "Note name is invalid", Toast.LENGTH_SHORT).show();
            } else {
                // note name is valid
                Note note = new Note(name, getCurrentTimeAMPM(),imagePath);
                int id = (int)noteDb.createNote(note);
                note.setNoteId(id);
                Intent intent = new Intent(CreateActivity.this, EditActivity.class);
                intent.putExtra("note",note);
                intent.putExtra("new", true);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                CreateActivity.this.finish();
            }
        });


        // back button listener
        backButton.setOnTouchListener((view, motionEvent) -> {
            finish();
            return true;
        });

    }

    private void loadCoverImage(){
        Glide.with(imageViewBox).load(imagePath).into(noteCover);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data, @NonNull ComponentCaller caller) {
        super.onActivityResult(requestCode, resultCode, data, caller);

        if (requestCode == CoverUploader.PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
            if (data != null && data.getData() != null) {
                Uri imageUri = data.getData();
                imagePath = coverUploader.saveImageToInternalStorage(imageUri);

                if (imagePath != null) {
                    Log.d(TAG, "Full path of image:  " + imagePath);
                    loadCoverImage();
                } else {
                    Log.e(TAG, "Saving failed");
                }
            }
        }

    }

    public static String getCurrentTimeAMPM() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
        return sdf.format(new Date());
    }


    private void findViews() {
        createNote = findViewById(R.id.create_note);
        noteCover = findViewById(R.id.note_cover);
        noteName = findViewById(R.id.note_name_input);
        backButton = findViewById(R.id.layout_create_back_button);
    }


}